package com.monlyu.action.system;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.monlyu.action.BaseAction;
import com.monlyu.bo.RoleBo;
import com.monlyu.vo.Permission;
import com.monlyu.vo.Role;
import com.monlyu.vo.Role2Permission;

/********************************************************************************
 * 
 * <b>功能描述☞</b>角色信息编辑维护<br>
 * 
 * <b>开发公司☞</b>重庆唯尼科技有限公司<br>
 * <b>版权☞</b>本文件归重庆唯尼科技有限公司所有<br>
 * <b>项目名☞</b>NewsSys<br>
 * <b>文件路径☞</b>com.monlyu.action.system.EditRoleAction.java<br>
 * <b>开发日期☞</b>2010-2-11 <br>
 * 
 * @author monlyu
 * @since JDK 5
 * @version 1.0.8
 * 
 ********************************************************************************/
public class EditRoleAction extends BaseAction {

	private Role role;
	private RoleBo roleBo;
	private String[] permiss;
	private String perstr;
	private static List<Role2Permission> ROLE2PERMIS = Lists.newArrayList();

	@Override
	public String execute() throws Exception {
		String flag = "u";
		if (role.getId() == null) {
			flag = "a";
		}
		roleBo.save(role, permiss);
		return ajax(flag + role.getId());
	}

	public String toEdit() {
		if (role != null && role.getId() != null) {
			roleBo.load(role);
			ROLE2PERMIS = roleBo.getPermByRId(role.getId());
		}
		perstr = buildPersim(roleBo.getPermiss());
		return view("editRole.jsp");
	}

	private String buildPersim(List<Permission> list) {
		Map<String, List<Permission>> map = Maps.newHashMap();
		for (Permission permission : list) {
			List<Permission> lists = map.get(permission.getType());
			if (lists == null) {
				lists = Lists.newArrayList();
			}
			lists.add(permission);
			map.put(permission.getType(), lists);
		}
		StringBuilder builder = new StringBuilder();
		for (Entry<String, List<Permission>> entry : map.entrySet()) {
			builder.append("<div class=\"b2b\"><span>(");
			builder.append(entry.getKey());
			builder.append(")<input type='checkbox' class='checkAll' />全选</span>");
			for (Permission permission : entry.getValue()) {
				builder.append("<input type=\'checkbox\' name=\'permiss\' value=\'");
				builder.append(permission.getId());
				builder.append("\'");
				builder.append(check(permission.getId()));
				builder.append("\'/>");
				builder.append(permission.getDesc());
			}
			builder.append("</div>");
		}
		ROLE2PERMIS = Lists.newArrayList();
		return builder.toString();
	}

	/**
	 * 是否检查
	 */
	private String check(String rid) {
		if (!ROLE2PERMIS.isEmpty()) {
			for (Role2Permission r2p : ROLE2PERMIS) {
				if (r2p.getPermissionId().equals(rid)) {
					return "checked=\"checked\"";
				}
			}
		}
		return "";
	}

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	public void setRoleBo(RoleBo roleBo) {
		this.roleBo = roleBo;
	}

	public String getPerstr() {
		return perstr;
	}

	public String[] getPermiss() {
		return permiss;
	}

	public void setPermiss(String[] permiss) {
		this.permiss = permiss;
	}

	public void setPerstr(String perstr) {
		this.perstr = perstr;
	}
}